<?php
namespace Home\Controller;
use Think\Controller;
class MasterController extends Controller {
    function  upload(){
        // 上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
        //// 上传单个文件
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }

        return $info;
    }
    public function index(){
        // 列表
        $master = D('master');
        $list = $master->select();
        $this->assign(array(
            'list'        => $list
        ));
        $this->display();
    }

    function del(){
        // 删除
        $master = D('master');
        $id = I('get.id');
        $de = $master->where("id='$id'")->delete();
        // $json = array('success' =>$de);
        // echo json_encode($json);
        if($de==1){
            $this -> success('删完啦');
        }else{
            $this -> error('出错了');
        }
    }

    function xq(){
        // 详情
        $master = D('master');
        $id = I('get.id');
        $info = $master -> where("id='$id'")->find();
        $this->assign('data',$info);
        $this->display();
    }

    function edit(){
        //  $id=I('get.id');
        // 修改
        $master = D('master');
        $id = I('get.id');
        $info = $master -> where("id='$id'")->find();
        $this->assign('data',$info);
        $this->display();
    }
    function add(){
        $this->display();
    }
    function doAdd(){
        // 添加
        $info=$this->upload();
        $master=D('master');
        $data['master_img']=$info['savepath'].$info['savename'];
        if($vo = $master->create()) {
            $master->master_img=$data['master_img'];
            $result = $master->add();
            if($result !== false){
                $this -> success('数据添加成功！');
            }else{
                $this -> error('数据添加失败！');
            }
        }else{
            $this->error($master->getError());
        }
    }
    function doEdit(){
        // 修改
        $info=$this->upload();
        $master=D('master');
        $data['master_img']=$info['savepath'].$info['savename'];
        if($vo = $master->create()) {
            $master->master_img=$data['master_img'];
            $result = $master->save();
            if($result !== false){
                $this -> success('数据更新成功！');
            }else{
                $this -> error('数据更新失败！');
            }
        }else{
            $this->error($master->getError());
        }
    }
}